package com.demo.starter.domain;

import lombok.Data;

import java.io.Serializable;

/**
 * API 返回结果封装
 * @param <T>
 */
@Data
public class JsonResult<T> implements Serializable {

    private static final int OK = 0;
    private static final int FAIL = 1;

    private T data; //服务端数据
    private int status = OK; //状态码
    private String msg = ""; //描述信息

    //APIS
    public static JsonResult OK() {
        return new JsonResult();
    }

    public static JsonResult FAIL() {
        return new JsonResult().status(FAIL);
    }

    public static JsonResult FAIL(Throwable e) {
        return FAIL().msg(e);
    }

    public JsonResult msg(Throwable e) {
        this.setMsg(e.toString());
        return this;
    }

    public JsonResult data(T data) {
        this.setData(data);
        return this;
    }

    public JsonResult status(int status) {
        this.setStatus(status);
        return this;
    }

}
